// axios 二次封装
import axios from 'axios'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const requsets = axios.create({
    // baseURL:'http://localhost:3001',
    // baseURL:'http://172.17.147.23:3001',
    // baseURL:'http://106.55.165.19:3001',
    timeout:5000
})

requsets.defaults.headers.common["authorization"] =  localStorage.getItem('token')

// 请求拦截器
requsets.interceptors.request.use((config)=>{
    nprogress.start()
    requsets.defaults.headers.common["authorization"] =  localStorage.getItem('token')
    return config
})

// 响应拦截器
requsets.interceptors.response.use((res)=>{
    nprogress.done()
    // console.log(res.data)
    if(res.data.flag == false && (res.data.msg=="未登录，请先登录" || res.data.msg=="授权过期")){
        linkToLogin(res.data.msg)
        localStorage.removeItem('token')
    }
    return res.data
},(error)=>{
    nprogress.done()
    return Promise.reject(error)
})

export default requsets